<?php

namespace App\Kuafu\Banner\Service;

use App\Kuafu\Banner\Enum\BannerCacheEnum;
use App\Kuafu\Banner\Enum\BannerCodeEnum;
use App\Kuafu\Banner\Model\BannerConfigModel;
use App\Kuafu\Banner\Exceptions\BannerException;
use App\Kuafu\User\Service\AuthService;
use Illuminate\Support\Facades\Cache;
use App\Kuafu\Base\Service\BaseService;

class BannerConfigService extends BaseService
{

    /**
     * 横幅详情
     *
     * @param int $id
     * @return array
     * @throws BannerException
     * @throws \App\Kuafu\User\Exceptions\UserException
     */
    public static function getBannerById(int $id): array
    {
        $getBannerById = Cache::remember(BannerCacheEnum::getBannerCacheById($id), 10, function () use ($id) {
            return BannerConfigModel::query()->find($id);
        });

        if (!$getBannerById) {
            return [];
        }

        return $getBannerById->toArray();
    }

    /**
     * 横幅列表
     *
     * @param int $pageSize
     * @param int $pageNum
     * @return array
     */
    public static function getBannerList(int $pageSize, int $pageNum): array
    {
        $query = BannerConfigModel::getDataPage($pageSize, $pageNum);
        return [
            'pageNum'   => $query->currentPage(),
            'pageSize'  => $query->perPage(),
            'pageCount' => ceil($query->total() / $query->perPage()),
            'count'     => $query->total(),
            'lists'     => $query->items(),
        ];
    }

    /**
     * 横幅添加
     *
     * @param array $data
     * @throws BannerException
     * @throws \App\Kuafu\User\Exceptions\UserException
     */
    public static function addBannerConfig(array $data): void
    {
        $data['updatedByUid'] = AuthService::getUserId();
        $data['createdByUid'] = AuthService::getUserId();
        $s                    = BannerConfigModel::addData($data);
        if (!$s) {
            throw new BannerException(BannerCodeEnum::ERR_BANNER_DB_ADD);
        }
    }

    /**
     * 横幅修改
     * @param $request
     * @throws BannerException|\App\Kuafu\User\Exceptions\UserException
     */
    public static function setBannerConfig($request)
    {
        $data = [
            "name"         => $request->name,
            "desc"         => $request->desc,
            "updatedByUid" => AuthService::getUserId(),
            'updatedAt'    => time()
        ];
        $id   = BannerConfigModel::setData($request->id, $data);
        if (!$id) {
            throw new BannerException(BannerCodeEnum::ERR_BANNER_DB_SET, '编辑失败');
        }

    }

    /**
     * 横幅删除
     * @param int $id
     * @throws BannerException
     */
    public static function delBanner(int $id): void
    {
        $query = BannerConfigModel::query()->find($id);
        if (!$query) {
            throw new BannerException(BannerCodeEnum::ERR_BANNER_ID_NOT_EXIST);
        }

        $delBanner = BannerConfigModel::delData($id);
        if (!$delBanner) {
            throw new BannerException(BannerCodeEnum::ERR_BANNER_DB_DEL);
        }
    }

    public static function patchBannerStatus(int $id, int $status)
    {
        $query = BannerConfigModel::query()->find($id);
        if (!$query) {
            throw new BannerException(BannerCodeEnum::ERR_BANNER_ID_NOT_EXIST);
        }

        $s = BannerConfigModel::setStatus($id, $status);
        if (!$s) {
            throw new BannerException(BannerCodeEnum::ERR_BANNER_DB_PATCH);
        }
    }
}
